

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.datapull.motu &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.datapull.motu</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.datapull.motu</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with Frommle; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018</span>

<span class="kn">from</span> <span class="nn">geoslurp.datapull</span> <span class="k">import</span> <span class="n">CrawlerBase</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull</span> <span class="k">import</span> <span class="n">UriBase</span><span class="p">,</span><span class="n">UriFile</span>
<span class="kn">from</span> <span class="nn">geoslurp.tools.Bounds</span> <span class="k">import</span> <span class="n">BtdBox</span>
<span class="kn">from</span> <span class="nn">geoslurp.tools.netcdftools</span> <span class="k">import</span> <span class="n">stackNcFiles</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">from</span> <span class="nn">dateutil.parser</span> <span class="k">import</span> <span class="n">parse</span> <span class="k">as</span> <span class="n">isoParser</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">motu_utils.motu_api</span> <span class="k">import</span> <span class="n">execute_request</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">lxml</span> <span class="k">import</span> <span class="n">etree</span> <span class="k">as</span> <span class="n">XMLTree</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">netCDF4</span> <span class="k">import</span> <span class="n">num2date</span>
<span class="kn">import</span> <span class="nn">copy</span>


<div class="viewcode-block" id="MotuOpts"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuOpts">[docs]</a><span class="k">class</span> <span class="nc">MotuOpts</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;A class which mimics the options from argparse as used by the motuclient command line program&quot;&quot;&quot;</span>
    <span class="c1">#list all configurable options here</span>
    <span class="n">auth_mode</span><span class="o">=</span><span class="s1">&#39;cas&#39;</span>
    <span class="n">user</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">pwd</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">motu</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">product_id</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">service_id</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">latitude_max</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">latitude_min</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">longitude_min</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">longitude_max</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">depth_max</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">depth_min</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">variable</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">out_dir</span><span class="o">=</span><span class="s1">&#39;.&#39;</span>
    <span class="n">cache</span><span class="o">=</span><span class="s1">&#39;.&#39;</span>
    <span class="n">out_name</span><span class="o">=</span><span class="s1">&#39;dataset.nc&#39;</span>
    <span class="n">describe</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">extraction_geographic</span><span class="o">=</span><span class="kc">True</span>
    <span class="n">extraction_vertical</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">proxy_server</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">outputWritten</span><span class="o">=</span><span class="s2">&quot;netcdf&quot;</span>
    <span class="n">console_mode</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">socket_timeout</span><span class="o">=</span><span class="mi">515</span>
    <span class="n">sync</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">block_size</span><span class="o">=</span><span class="mi">12001</span>
    <span class="n">size</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">date_min</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">min</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
    <span class="n">date_max</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">max</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
    <span class="n">user_agent</span><span class="o">=</span><span class="s1">&#39;motu-api-client&#39;</span>
    <span class="c1">#custom variables</span>
    <span class="n">btdbox</span><span class="o">=</span><span class="n">BtdBox</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">moturoot</span><span class="p">,</span><span class="n">service</span><span class="p">,</span><span class="n">product</span><span class="p">,</span><span class="n">auth</span><span class="p">,</span><span class="n">btdbox</span><span class="p">,</span><span class="n">fout</span><span class="p">,</span><span class="n">cache</span><span class="p">,</span><span class="n">variables</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sets options&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">motu</span><span class="o">=</span><span class="n">moturoot</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">service_id</span><span class="o">=</span><span class="n">service</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">product_id</span><span class="o">=</span><span class="n">product</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user</span><span class="o">=</span><span class="n">auth</span><span class="o">.</span><span class="n">user</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pwd</span><span class="o">=</span><span class="n">auth</span><span class="o">.</span><span class="n">passw</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">(</span><span class="n">btdbox</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cache</span><span class="o">=</span><span class="n">cache</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variable</span><span class="o">=</span><span class="n">variables</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">syncfilename</span><span class="p">(</span><span class="n">fout</span><span class="p">)</span>

<div class="viewcode-block" id="MotuOpts.syncbtdbox"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuOpts.syncbtdbox">[docs]</a>    <span class="k">def</span> <span class="nf">syncbtdbox</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">bbox</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sets the internal btdbox and synchronize the corresponding motu variables&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">bbox</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">=</span><span class="n">bbox</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">latitude_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">s</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">latitude_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">n</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">longitude_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">w</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">longitude_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">e</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">ts</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">date_min</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">ts</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">te</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">date_max</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">te</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MotuOpts.syncfilename"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuOpts.syncfilename">[docs]</a>    <span class="k">def</span> <span class="nf">syncfilename</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">fout</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">fout</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">fout</span><span class="p">)</span></div>

<div class="viewcode-block" id="MotuOpts.fullname"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuOpts.fullname">[docs]</a>    <span class="k">def</span> <span class="nf">fullname</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out_dir</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">out_name</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="Uri"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.Uri">[docs]</a><span class="k">class</span> <span class="nc">Uri</span><span class="p">(</span><span class="n">UriBase</span><span class="p">):</span>
    <span class="n">kbsize</span><span class="o">=</span><span class="mi">0</span>
    <span class="n">maxkbsize</span><span class="o">=</span><span class="mi">0</span>
    <span class="n">info</span><span class="o">=</span><span class="kc">False</span>
    <span class="n">maxbtdbox</span><span class="o">=</span><span class="n">BtdBox</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">Mopts</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">=</span><span class="n">Mopts</span>
        <span class="n">url</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">Mopts</span><span class="o">.</span><span class="n">out_dir</span><span class="p">,</span><span class="n">Mopts</span><span class="o">.</span><span class="n">out_name</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>

<div class="viewcode-block" id="Uri.requestInfo"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.Uri.requestInfo">[docs]</a>    <span class="k">def</span> <span class="nf">requestInfo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Request info (modification time, size, datacoverage) on this specific query from the server&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">info</span><span class="p">:</span>
            <span class="c1">#quick return when already done</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">describe</span><span class="o">=</span><span class="kc">True</span>
        <span class="n">oldd</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span>
        <span class="n">oldnm</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">cache</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.nc&#39;</span><span class="p">,</span><span class="s1">&#39;_descr.xml&#39;</span><span class="p">)</span>
        <span class="c1"># import pdb;pdb.set_trace()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">execute_request</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;failed to request info on query&quot;</span><span class="p">)</span>
            <span class="k">raise</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">describe</span><span class="o">=</span><span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">oldd</span>

        <span class="c1">#read and parse xml</span>
        <span class="n">xml</span><span class="o">=</span><span class="n">XMLTree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">cache</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.nc&#39;</span><span class="p">,</span><span class="s1">&#39;.xml&#39;</span><span class="p">)))</span>
        <span class="n">trange</span><span class="o">=</span><span class="n">xml</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;timeCoverage&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span><span class="o">=</span><span class="n">isoParser</span><span class="p">(</span><span class="n">trange</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;end&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>

        <span class="c1">#also retrieve datacoverage</span>
        <span class="n">covdict</span><span class="o">=</span><span class="p">{}</span>
        <span class="k">for</span> <span class="n">axis</span> <span class="ow">in</span> <span class="n">xml</span><span class="o">.</span><span class="n">iterfind</span><span class="p">(</span><span class="s1">&#39;dataGeospatialCoverage/axis&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;axisType&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Lat&#39;</span><span class="p">:</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;s&#39;</span><span class="p">]</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;lower&#39;</span><span class="p">])</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;n&#39;</span><span class="p">]</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;upper&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;axisType&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Lon&#39;</span><span class="p">:</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;w&#39;</span><span class="p">]</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;lower&#39;</span><span class="p">])</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;upper&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;axisType&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;Time&#39;</span><span class="p">:</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;ts&#39;</span><span class="p">]</span><span class="o">=</span><span class="n">num2date</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;lower&#39;</span><span class="p">]),</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;units&#39;</span><span class="p">])</span>
                <span class="n">covdict</span><span class="p">[</span><span class="s1">&#39;te&#39;</span><span class="p">]</span><span class="o">=</span><span class="n">num2date</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;upper&#39;</span><span class="p">]),</span><span class="n">axis</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;units&#39;</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">maxbtdbox</span><span class="o">=</span><span class="n">BtdBox</span><span class="p">(</span><span class="o">**</span><span class="n">covdict</span><span class="p">)</span>

        <span class="c1">#Crop/Synchronize the requested bounding box with that what is available</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">crop</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">maxbtdbox</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">()</span>



        <span class="c1">#hack (change outname back to nc suffix)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="n">oldnm</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">info</span><span class="o">=</span><span class="kc">True</span></div>


<div class="viewcode-block" id="Uri.updateModTime"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.Uri.updateModTime">[docs]</a>    <span class="k">def</span> <span class="nf">updateModTime</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Requests data description from the motu service&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">info</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">requestInfo</span><span class="p">()</span></div>

<div class="viewcode-block" id="Uri.updateSize"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.Uri.updateSize">[docs]</a>    <span class="k">def</span> <span class="nf">updateSize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Request information about the size of the query&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">size</span><span class="o">=</span><span class="kc">True</span>
        <span class="n">oldd</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">cache</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">execute_request</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;failed to request size: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span><span class="n">e</span><span class="p">)</span>
            <span class="k">raise</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
        <span class="c1"># self.opts.out_name=self.opts.out_name.replace(&#39;.nc&#39;,&#39;.xml&#39;)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">size</span><span class="o">=</span><span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">oldd</span>

        <span class="n">xml</span><span class="o">=</span><span class="n">XMLTree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">cache</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kbsize</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">xml</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;size&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">maxkbsize</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">xml</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;maxAllowedSize&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.xml&#39;</span><span class="p">,</span><span class="s1">&#39;.nc&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">kbsize</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">maxkbsize</span></div>

<div class="viewcode-block" id="Uri.download"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.Uri.download">[docs]</a>    <span class="k">def</span> <span class="nf">download</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">direc</span><span class="p">,</span><span class="n">check</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">gzip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span><span class="n">outfile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1">#check whether the file exists and retrive thelast update date</span>


        <span class="k">if</span> <span class="n">outfile</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="n">outfile</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">direc</span>

        <span class="n">fout</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">direc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">out_name</span><span class="p">)</span>

        <span class="n">uri</span><span class="o">=</span><span class="n">UriFile</span><span class="p">(</span><span class="n">fout</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">check</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">fout</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updateModTime</span><span class="p">()</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastmod</span> <span class="o">&lt;=</span> <span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
                <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;No need to download file </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">fout</span><span class="p">))</span>
                <span class="k">return</span> <span class="n">uri</span><span class="p">,</span><span class="kc">False</span>



        <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Downloading </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">fout</span><span class="p">))</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">execute_request</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opts</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;failed to download file </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span><span class="n">e</span><span class="p">)</span>
            <span class="k">raise</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">uri</span><span class="p">,</span><span class="kc">True</span></div></div>


<div class="viewcode-block" id="MotuRecursive"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuRecursive">[docs]</a><span class="k">class</span> <span class="nc">MotuRecursive</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Class which recursively downloads netcdf files within the 1GB limit using motu and patches them together&quot;&quot;&quot;</span>
    <span class="n">keepfiles</span><span class="o">=</span><span class="kc">False</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">mopts</span><span class="p">,</span><span class="n">keepfiles</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">=</span><span class="n">mopts</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">keepfiles</span><span class="o">=</span><span class="n">keepfiles</span>

<div class="viewcode-block" id="MotuRecursive.download"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.motu.MotuRecursive.download">[docs]</a>    <span class="k">def</span> <span class="nf">download</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Download file&quot;&quot;&quot;</span>
        <span class="n">muri</span><span class="o">=</span><span class="n">Uri</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="p">)</span>

        <span class="c1">#check if download is needed</span>
        <span class="n">muri</span><span class="o">.</span><span class="n">requestInfo</span><span class="p">()</span>
        <span class="n">uristacked</span><span class="o">=</span><span class="n">UriFile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">uristacked</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">muri</span><span class="o">.</span><span class="n">lastmod</span> <span class="o">&lt;=</span> <span class="n">uristacked</span><span class="o">.</span><span class="n">lastmod</span><span class="p">:</span>
                <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Already downloaded </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uristacked</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
                <span class="c1">#quick return when there is no need to merge/download</span>
                <span class="k">return</span> <span class="n">uristacked</span><span class="p">,</span><span class="kc">False</span>

        <span class="c1">#check if download is allowed</span>
        <span class="n">kb</span><span class="p">,</span><span class="n">maxkb</span><span class="o">=</span><span class="n">muri</span><span class="o">.</span><span class="n">updateSize</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">kb</span> <span class="o">&gt;</span> <span class="n">maxkb</span><span class="p">:</span>
            <span class="c1">#split up request and try again</span>

            <span class="c1">#create 2 bounding boxes split on time</span>
            <span class="n">Abbox</span><span class="p">,</span><span class="n">Bbbox</span><span class="o">=</span><span class="n">muri</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">btdbox</span><span class="o">.</span><span class="n">timeSplit</span><span class="p">()</span>

            <span class="n">AmotuRec</span><span class="o">=</span><span class="n">MotuRecursive</span><span class="p">(</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="p">))</span>
            <span class="n">AmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">(</span><span class="n">Abbox</span><span class="p">)</span>
            <span class="n">AmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.nc&#39;</span><span class="p">,</span><span class="s1">&#39;_A.nc&#39;</span><span class="p">)</span>
            <span class="n">AmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">AmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">cache</span>

            <span class="n">BmotuRec</span><span class="o">=</span><span class="n">MotuRecursive</span><span class="p">(</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="p">))</span>
            <span class="n">BmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">syncbtdbox</span><span class="p">(</span><span class="n">Bbbox</span><span class="p">)</span>
            <span class="n">BmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_name</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.nc&#39;</span><span class="p">,</span><span class="s1">&#39;_B.nc&#39;</span><span class="p">)</span>
            <span class="n">BmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_dir</span><span class="o">=</span><span class="n">BmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">cache</span>

            <span class="n">Auri</span><span class="p">,</span><span class="n">Aupd</span><span class="o">=</span><span class="n">AmotuRec</span><span class="o">.</span><span class="n">download</span><span class="p">()</span>
            <span class="n">Buri</span><span class="p">,</span><span class="n">Bupd</span><span class="o">=</span><span class="n">BmotuRec</span><span class="o">.</span><span class="n">download</span><span class="p">()</span>

            <span class="c1">#possible improvement here split a dataset at an unlimited dimensions and append the second one to the first one</span>
            <span class="c1">#patch files together (if updated)</span>
            <span class="k">if</span> <span class="n">Aupd</span> <span class="ow">or</span> <span class="n">Bupd</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">()):</span>
                <span class="n">uristacked</span><span class="p">,</span><span class="n">upd</span><span class="o">=</span><span class="n">stackNcFiles</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">(),</span><span class="n">Auri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="n">Buri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span><span class="s1">&#39;time&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">keepfiles</span><span class="p">:</span>
                    <span class="c1">#remove the partial files</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">AmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">())</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">BmotuRec</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">())</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">uristacked</span><span class="o">=</span><span class="n">UriFile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">fullname</span><span class="p">())</span>
                <span class="n">upd</span><span class="o">=</span><span class="kc">False</span>
            <span class="k">return</span> <span class="n">uristacked</span><span class="p">,</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">muri</span><span class="o">.</span><span class="n">download</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mopts</span><span class="o">.</span><span class="n">out_dir</span><span class="p">,</span><span class="n">check</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div></div>

</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>